<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<title>Excel导入配置管理</title>
<script type="text/javascript">
	var $grid, $detailGrid, selectTableRow;//选中的行数据
	var editIndex = -1;
	$(function() {
		$grid = $("#grid").datagrid(
				$.extend({}, datagridOption, {
					url : "${ctx}/basic/excelImportForRowEdit/dataGrid",
					columns : [ [
							{
								field : 'ck',
								checkbox : true
							},
							{
								field : 'importNo',
								title : '导入编号',
								width : 120,
								sortable : true
							},
							{
								field : 'importName',
								title : '模板名称',
								width : 120,
								sortable : true
							},
							{
								field : 'tableName',
								title : '表名称',
								width : 120,
								sortable : true
							},
							{
								field : 'isOverride',
								title : '是否覆盖',
								width : 90,
								formatter : function(value, row, index) {
									return '<input class="easyui-switchbutton "' + (value == 1 ? "checked" : "")
											+ ' data-options="readonly:true,onText:\'是\',offText:\'否\'">';
								}
							}, {
								field : 'remark',
								title : '备注',
								width : 90,
								sortable : true
							} ] ],
					onDblClickRow : function(index, rowData) {
						fn_edit(rowData);
					}
				}));
		$grid.datagrid('enableFilter');
		$("#isOverrideSB").switchbutton({
			onText : "是",
			offText : "否",
			checked : true,
			onChange : function(checked) {
				if (checked) {
					$("#isOverride").val(1);
				} else {
					$("#isOverride").val(2);
				}
			}
		});

		$("#add").click(function() {
			$("#fm").form("clear");
			$('#fm').form('load', {
				enable : 1,
			});
			$("#isOverrideSB").switchbutton("check");
			$("#dlg").dialog("open").dialog("setTitle", "新增Excel导入");
			selectTableRow = null;
			//清除所有选中的行，解决切换不同代码表后还保留代码值中上次选中行bug
			$grid.datagrid("clearSelections");
			$detailGrid.datagrid("clearSelections");
			refreshDetail();//刷新代码表明细
		});

		$("#edit").click(function() {
			var rowData = $grid.datagrid("getSelected");
			if (rowData == null) {
				showInfo("您未选中行，无法编辑");
			} else {
				fn_edit(rowData);
			}
		});
		$("#delete").click(function() {
			multiDeleteDataGrid($grid, "${ctx}/basic/excelImportForRowEdit/multiDelete");
			$detailGrid.datagrid("reload");
		});
		$("#excelExport").click(function() {
			var url = "${ctx}/basic/excelImportForRowEdit/excelExport?" + $.param($grid.datagrid('options').queryParams);
			if ($grid.datagrid("options").filterRules) {
				url += "&filterRules=" + JSON.stringify($grid.datagrid("options").filterRules);
			}
			window.parent.location.href = url;
		});
		$("#excelImport").click(function() {
			$.easyui.importor.showImportor({
				importNo : "ExcelImport",
				onExportSuccess : function(file, data) {
					$grid.datagrid('load');
					$detailGrid.datagrid("load");
				}
			});
		});

		//-----------明细--------------------
		$detailGrid = $("#detailGrid").datagrid($.extend({}, datagridOption, {
			toolbar : '',
			columns : [ [ {
				field : 'ck',
				checkbox : true
			}, {
				field : 'chineseName',
				title : '中文列名',
				sortable : true,
				editor : "textbox"
			}, {
				field : 'columnName',
				title : '字段名称',
				sortable : true,
				editor : "textbox"
			}, {
				field : 'columnNo',
				title : '列序号',
				sortable : true,
				editor : "numberbox"
			}, {
				field : 'codeValues.id',
				title : 'Java类型',
				sortable : true,
				formatter : function(value, row, index) {
					row['javaType'] = value.id;
					return row && row.codeValues ? row.codeValues.codeName : "";
				},
				editor : {
					type : "combobox",
					options : {
						required : true,
						editable : false,
						url : "${ctx}/basic/excelConfig/comboboxJavaType",
						valueField : "id",
						textField : "codeName"
					}
				}
			}, {
				field : 'isUnionUnique',
				title : '是否联合唯一',
				formatter : function(value, row, index) {
					return value == 1 ? "是" : "否";
				},
				/**basicEasyUI.js中定义**/
				editor : comboboxEditorOption
			}, {
				field : 'isForeignKey',
				title : '是否为外键',
				formatter : function(value, row, index) {
					return value == 1 ? "是" : "否";
				},
				editor : comboboxEditorOption
			}, {
				field : 'foreignTableName',
				title : '关联表名称',
				sortable : true,
				editor : "textbox"
			}, {
				field : 'fkColumnName',
				title : '关联字段名称',
				sortable : true,
				editor : "textbox"
			}, {
				field : 'foreignTableAssistColumnName',
				title : '关联表的辅助字段名称',
				sortable : true,
				editor : "textbox"
			}, {
				field : 'isAssistColumn',
				title : '是否是辅助字段',
				formatter : function(value, row, index) {
					return value == 1 ? "是" : "否";
				},
				editor : comboboxEditorOption
			}, {
				field : 'assistForeignTableName',
				title : '辅助关联表名称',
				sortable : true,
				editor : "textbox"
			}, {
				field : 'assistFkColumnName',
				title : '辅助关联字段名称',
				sortable : true,
				editor : "textbox"
			}, {
				field : 'assistExcelColumnNo',
				title : '辅助值Excel列数',
				sortable : true,
				editor : "numberbox"
			}, {
				field : 'otherColumns',
				title : '其他关联字段集',
				sortable : true,
				editor : "textbox"
			}, {
				field : 'otherValues',
				title : '其他关联字段值',
				sortable : true,
				editor : "textbox"
			}, {
				field : 'convertMap',
				title : '转换Map',
				sortable : true,
				editor : "textbox"
			}, {
				field : 'remark',
				title : '备注',
				sortable : true,
				editor : "textbox"
			} ] ],
			autoEditingEvent : "onDblClickRow",
			autoEditing : true, //该属性启用双击行时自定开启该行的编辑状态
			extEditing : false, //该属性启用行编辑状态的 ExtEditing 风格效果，该属性默认为 true。
			singleEditing : false,

			onBeginEdit : function(rowIndex, rowData) {
				var _onBeginEdit = $.fn.datagrid.extensions.onBeginEdit;
				if ($.isFunction(_onBeginEdit)) {
					_onBeginEdit.apply(this, arguments);
				}
				editIndex = rowIndex;
				var codeValuesEditor = $detailGrid.datagrid('getEditor', {
					index : rowIndex,
					field : 'codeValues.id'
				});
				$(codeValuesEditor.target).combobox('setValue', rowData.codeValues.id);
			}
		}));
		$("#detailAdd").click(function() {
			if (endEditing()) {
				$detailGrid.datagrid('appendRow', {
					isForeignKey : 2,
					isUnionUnique : 2,
					isAssistColumn : 2
				});
				editIndex = $detailGrid.datagrid('getRows').length - 1;
				$detailGrid.datagrid('selectRow', editIndex).datagrid('beginEdit', editIndex);
			}
		});
		$("#redo").click(function() {
			$detailGrid.datagrid("rejectChanges");
		});

		$("#save").click(function() {
			if (endEditing()) {
				$detailGrid.datagrid("endEditAll");
				$("#fm").form("submit", {
					url : "${ctx}/basic/excelImportForRowEdit/update",
					onSubmit : function(param) {
						var inserted = $detailGrid.datagrid('getChanges', "inserted");
						var updated = $detailGrid.datagrid('getChanges', "updated");
						for (var i = 0; i < inserted.length; i++) {
							inserted[i].codeValues = {
								id : inserted[i]['codeValues.id']
							};
						}
						for (var i = 0; i < updated.length; i++) {
							updated[i].codeValues = {
								id : updated[i]['codeValues.id']
							};
						}
						$("#fm").form("load", {
							detailInserted : JSON.stringify(inserted),
							detailUpdated : JSON.stringify(updated),
							detailDeleted : JSON.stringify($detailGrid.datagrid('getChanges', "deleted"))
						});
					},
					success : function(data) {
						$grid.datagrid("load");
						$.messager.show({
							msg : "保存成功",
							icon : "info",
							position : "bottomRight"
						});
						$("#fm").form("load", data.obj);
						selectTableRow = data.obj;
						$detailGrid.datagrid("reload");
					}
				});
			}
		});
		$("#detailDelete").click(function() {
			multiDeleteDataGrid($detailGrid, "${ctx}/basic/excelConfig/multiDelete");
		});
		$("#detailExcelExport").click(
				function() {
					if (selectTableRow != null) {
						var url = "${ctx}/basic/excelImportForRowEdit/excelExportConfig?search_EQ_excelImport.id=" + selectTableRow.id
								+ "&search_LIKE_columnName=" + $("#search_LIKE_columnName").val();
					} else {
						var search_LIKE_columnName = $("#search_LIKE_columnName").val();
						if (search_LIKE_columnName != null) {
							var url = "${ctx}/basic/excelImportForRowEdit/excelExportConfig?search_LIKE_columnName="
									+ $("#search_LIKE_columnName").val();
						} else {
							var url = "${ctx}/basic/excelImportForRowEdit/excelExportConfig?"
									+ $.param($grid.datagrid('options').queryParams);
						}
					}
					if ($grid.datagrid("options").filterRules) {
						url += "&filterRules=" + JSON.stringify($grid.datagrid("options").filterRules);
					}
					window.parent.location.href = url;
				});
		$("#excelImportConfig").click(function() {
			$.easyui.importor.showImportor({
				importNo : "ExcelConfig",
				onExportSuccess : function(file, data) {
					$grid.datagrid('load');
					$detailGrid.datagrid("load");
				}
			});
		});

		$("#showAll").click(function() {
			$detailGrid.datagrid("clearSelections");//清除所有选中的行，解决始终保留代码值中上次选中行bug
			$detailGrid.datagrid({
				url : "${ctx}/basic/excelConfig/dataGrid"
			});
		});
	});
	function fn_edit(rowData) {
		$("#dlg").dialog("open").dialog("setTitle", "编辑Excel导入");
		$("#fm").form("load", rowData);
		$("#isOverrideSB").switchbutton(rowData.isOverride == 1 ? "check" : "uncheck");

		selectTableRow = rowData;
		//清除所有选中的行，解决切换不同代码表后还保留代码值中上次选中行bug
		$detailGrid.datagrid("clearSelections");
		refreshDetail(rowData);//刷新明细
	}
	//----------------明细----------------------------
	function endEditing() {
		if (editIndex == -1) {
			return true
		}
		if ($detailGrid.datagrid('validateRow', editIndex)) {
			$detailGrid.datagrid('endEdit', editIndex);
			editIndex = -1;
			return true;
		} else {
			return false;
		}
	}
	//刷新Excel导入明细列表
	function refreshDetail(rowData) {
		$detailGrid.datagrid({
			url : "${ctx}/basic/excelConfigForRowEdit/dataGrid?search_EQ_excelImport.id=" + (rowData ? rowData.id : "empty")
		});
	}
</script>
</head>

<body>
	<div data-options="region:'center'" data-options="fit:true" style="padding: 1px;">
		<table id="grid">
		</table>
		<div id="tt" class="row">
			<div class="col-md-12">
				<shiro:hasPermission name="excelImportForRowEdit:update">
					<button id="add" type="button" class="btn btn-primary">
						<span class="glyphicon glyphicon-plus"></span> 新增
					</button>
					<button id="edit" type="button" class="btn btn-primary">
						<span class="glyphicon glyphicon-pencil"></span> 编辑
					</button>
				</shiro:hasPermission>
				<shiro:hasPermission name="excelImportForRowEdit:delete">
					<button id="delete" type="button" class="btn btn-danger">
						<span class="glyphicon glyphicon-minus"></span> 删除
					</button>
				</shiro:hasPermission>
				<shiro:hasPermission name="excelImportForRowEdit:import">
					<button id="excelImport" type="button" class="btn btn-primary">
						<span class="glyphicon glyphicon-open"></span> Excel导入
					</button>
				</shiro:hasPermission>
				<shiro:hasPermission name="excelImportForRowEdit:export">
					<button id="excelExport" type="button" class="btn btn-primary">
						<span class="glyphicon glyphicon-save"></span> Excel导出
					</button>
				</shiro:hasPermission>
			</div>
		</div>
		<shiro:hasPermission name="excelImportForRowEdit:update">
			<div id="dlg" class="easyui-dialog" style="width: 95%; height: 98%;"
				data-options="resizable:true,modal:true,toolbar:'#configtt',closed:true,maximizable:true">
				<div class="easyui-layout" data-options="fit:true">
					<div data-options="region:'north',split:true" style="height: 110px;">
						<form id="fm" method="post">
							<input type="hidden" name="id" />
							<input type="hidden" name="detailInserted" />
							<input type="hidden" name="detailUpdated" />
							<input type="hidden" name="detailDeleted" />
							<table id="tbl">
								<tr>
									<td><label>导入编号:</label></td>
									<td><input name="importNo" class="easyui-validatebox" data-options="required:true,validType:'length[3,10]'"></td>
									<td><label>模板名称:</label></td>
									<td><input name="importName" class="easyui-validatebox" data-options="required:true"></td>
									<td><label>表名称:</label></td>
									<td><input name="tableName" class="easyui-validatebox" data-options="required:true"></td>
								</tr>
								<tr>
									<td><label>是否覆盖:</label></td>
									<td><input id="isOverride" name="isOverride" type="hidden"> <input id="isOverrideSB"></td>
									<td><label>备注:</label></td>
									<td colspan="3"><input name="remark" class="easyui-textbox" data-options="width:435"></td>
								</tr>
							</table>
						</form>
					</div>
					<div data-options="region:'center'">
						<table id="detailGrid">
						</table>

					</div>
				</div>
			</div>
			<div id="configtt">
				<shiro:hasPermission name="config:update">
					<button id="save" type="button" class="btn btn-primary">
						<span class="glyphicon glyphicon-ok"></span> 保存
					</button>
					<button id="detailAdd" type="button" class="btn btn-primary">
						<span class="glyphicon glyphicon-plus"></span> 添加一行
					</button>
					<button id="redo" type="button" class="btn btn-primary">
						<span class="glyphicon glyphicon-remove-circle"></span> 取消
					</button>
				</shiro:hasPermission>
				<shiro:hasPermission name="config:delete">
					<button id="detailDelete" type="button" class="btn btn-danger">
						<span class="glyphicon glyphicon-minus"></span> 删除
					</button>
				</shiro:hasPermission>
				<button id="showAll" type="button" class="btn btn-primary">
					<span class="glyphicon glyphicon-search"></span> 显示全部
				</button>
				<shiro:hasPermission name="config:import">
					<button id="excelImport" type="button" class="btn btn-primary">
						<span class="glyphicon glyphicon-open"></span> Excel导入
					</button>
				</shiro:hasPermission>
				<shiro:hasPermission name="config:export">
					<button id="detailExcelExport" type="button" class="btn btn-primary">
						<span class="glyphicon glyphicon-save"></span> Excel导出
					</button>
				</shiro:hasPermission>
			</div>
		</shiro:hasPermission>
	</div>
</body>
</html>
